<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;

class MemberController extends CommonController {
	protected $table = 'user';
	protected $integral = 'integral';
	
	/*
	*会员管理
	*/
    public function lists(){
		
		if (!$_GET['p']) {
			$_GET['p'] = 1;
		}	
		
		$cache = 'myuser'.$_GET['p'];	
		unset($str);
		unset($data);
		//搜索
		if ($_GET['search_name'] || $_GET['search_status'] || $_GET['search_grade'] || $_GET['username']) {
			
			if ($_GET ['search_status']>=0) {				
				$arr_data ['status']  = $_GET ['search_status'] ;
				$str1 = ' and status=%d';
				$da_str1 = ','.$arr_data ['status'] ;				
			} else {
				$str1 = '';
				$da_str1 = '' ;	
			}
			
			if ($_GET ['search_grade']>=0) {				
				$arr_data ['gotegary']  = $_GET ['search_grade'] ;
				$str2  = ' and gotegary=%d';
				$da_str2 = ','.$arr_data ['gotegary'] ;
			} else {
				$str2  = '';
				$da_str2 = '' ;
			}
			
			if ($_GET ['username']) {								
				$arr_data [$_GET['search_name']]  = $_GET ['username'] ;
				$str3  = ' and '.$_GET['search_name'].'="%s"';
				$da_str3 = ','.$_GET ['username'] ;
			} else {
				$str3  = '' ;
				$da_str3 = '' ;
			}
			
			$da_str = '0'.$da_str1.$da_str2.$da_str3 ;
			
			$str = "id>%d".$str1.$str2.$str3;
			
			$data = explode(',',$da_str);
			
			//dump($str);
			//dump($data);die;
		    //强制刷新
		   _Refresh(1,$cache);
		   
		} else {
			$str = "id>%d";
			$data = array(0);			
		}
		$num = 15;
		$order = 'id desc';
		//integral
		$join = 'RIGHT JOIN __INTEGRAL__ ON __INTEGRAL__.userid = __USER__.id';
		//分页
		$show = Page($this->table,$str,$data,$num,$join);
		
		//强制刷新
		_Refresh($_GET['clear'],$cache);
	
		//关联查询
		$list=_getJoinList($this->table,$join,$str,$data,$cache,$order,$_GET['p'],$num);
		
		//dump($list);die;
				
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('list',$list);
        $this->display();
    }

	/*
	*会员添加
	*
	*/
    public function addUser(){
	
		if (IS_POST) {

			unset($data);
			I('post.username')?$data['username'] = I('post.username'):$this->error('会员账号不能为空！','lists');
			//I('post.password')?$data['password'] = MD5(I('post.password')):$data['password'] = MD5('123456');
			I('post.password')?$data['password'] = _enPasswd(MD5(I('post.password'))):$data['password'] = _enPasswd(MD5('123456'));
			//var_dump($user);die;
			$data['mobile'] = I('post.mobile');//手机
			$data['realname'] = I('post.realname');//真实姓名
			$data['createtime'] = time();//注册时间
			
			$where_1 ['username'] = $data['username'];
			$find_one = _getOne($this->table,$where_1);
			if ($find_one) {
				$this->error('会员账号已存在！','lists');
			}
			
			$where_2 ['mobile'] = $data['mobile'];
			$data['mobile']? $find_tow = _getOne($this->table,$where_2): $find_tow = 0;
			if ($find_tow) {
				$this->error('手机号码已被注册！','lists');
			}
			
			if (!$_GET['p']) {
				$_GET['p'] = 1;
			}
			$cache = 'myuser'.$_GET['p'];

			$add = _getAdd($this->table,$data,$cache);
			
			$data2['userid'] = $add;
			
			$add2 = _getAdd($this->integral,$data2);
			
			if ($add2) {
				//强制刷新
				_Refresh(1,$cache);
				unset($data);	
				$this->success('注册成功','lists');				
			} else {
				$this->error('注册失败！','lists');
			}			
		}
    }
	
	/*
	*会员修改
	*
	*/
    public function editUser(){
	
		if (IS_POST) {
			
			if (!$_GET['p']) {
				$_GET['p'] = 1;
				$url = 'lists';
			} else {
				$url = 'lists/p/'.$_GET['p'];
			}			
			 $cache = 'myuser'.$_GET['p'];
			 
			 if (I('post.type')=="user_edit") {
				//修改会员信息					
				I('post.realname')?$data['realname'] = I('post.realname'):$a='';
				$data['mobile'] = I('post.mobile');
				
				I('post.password') && $data['password'] = _enPasswd(MD5(I('post.password'))) ;
				
				$where_1['id'] =array('NEQ',I('post.id')) ; 
				$where_1['mobile'] = I('post.mobile');												
				$data['mobile']? $find_tow = _getOne($this->table,$where_1): $find_tow = 0;
	
				//dump($_GET);die;					
				if ($find_tow) {
					$this->error('手机号码已被注册！',$url);
				}
				
				$where['id'] = I('post.id');
				$row = _getOne($this->table,$where);
				if (!$row) {
					$this->success('会员不存在',$url);
				}

				$edit = _getEdit($this->table,$where,'',$data,$cache) ;
				
				if ($edit) {
					//强制刷新
					_Refresh(1,$cache);
					unset($data);
					unset($where);					
					$this->success('修改成功',$url);				
				} else {
					$this->error('修改失败！',$url);
				}
			} elseif (I('post.state')) {

				//修改会员状态
				if (I('post.state')=='2') {
					$data['status'] = 1;
				} else {
					$data['status'] = I('post.state');
				}
				
				$where['id'] = I('post.id');	
				$stype=1;
				
				if ($stype) {
					//强制刷新
					_Refresh(1,$cache);
					
					$edit = _getEdit($this->table,$where,'',$data,$cache) ;
					
					if ($edit) {
						unset($data);
						unset($where);
						$res =I('post.state');
					} else {
						$res=4;
					}					
					unset($stype);
					unset($where);
					unset($data);								
				}
				
				if (I('post.state')) {					
					$this->ajaxReturn(array('info'=>$res));				
				}								
			}
		}
    }
	
}